package com.podium.web.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.web.log.entity.SysOperateLog;
import com.podium.web.log.mapper.SysOperateLogMapper;
import com.podium.web.log.service.SysOperateLogService;
import com.podium.comment.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 邵文泽
 * @description 系统操作日志表
 * @date 2025-05-12 09:05:37
 */
@Slf4j
@Service
public class SysOperateLogServiceImpl extends ServiceImpl<SysOperateLogMapper, SysOperateLog> implements SysOperateLogService {

    @Override
    public IPage<SysOperateLog> page(Page<SysOperateLog> page, SysOperateLog sysOperateLog) {
        QueryWrapper<SysOperateLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.hasLength(sysOperateLog.getUserName()), SysOperateLog::getUserName, sysOperateLog.getUserName())
                .eq(StringUtils.hasLength(sysOperateLog.getOperateType()), SysOperateLog::getOperateType, sysOperateLog.getOperateType())
                .orderByDesc(SysOperateLog::getCreateTime)
        ;
        return super.page(page, queryWrapper);
    }

    @Override
    public List<SysOperateLog> list(SysOperateLog sysOperateLog) {
        QueryWrapper<SysOperateLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.hasLength(sysOperateLog.getUserName()), SysOperateLog::getUserName, sysOperateLog.getUserName())
                .eq(StringUtils.hasLength(sysOperateLog.getOperateType()), SysOperateLog::getOperateType, sysOperateLog.getOperateType())
                .orderByDesc(SysOperateLog::getCreateTime)
        ;
        return super.list(queryWrapper);
    }

    @Override
    public SysOperateLog getById(String id) {
        return super.getById(id);
    }

    @Override
    public boolean save(SysOperateLog sysOperateLog) {
        sysOperateLog.setCreateTime(LocalDateTime.now());
        return super.save(sysOperateLog);
    }

    /**
     * 异步保存操作日志
     *
     * @param sysOperateLog 日志
     */
    @Override
    @Async("logTaskExecutor")
    public void asyncSaveLog(SysOperateLog sysOperateLog) {
        super.save(sysOperateLog);
    }

    @Override
    public boolean updateById(SysOperateLog sysOperateLog) {
        return super.updateById(sysOperateLog);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

}